const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const user = require('../../../../services/user.js');

var app = getApp();

Page({
  data: {
    tmChemReagents: [],
    reagentIds: [],
    cartTotal: {
      "checkedGoodsCount": 0
    },
    isEditCart: false,
    checkedAllStatus: true
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    if (currPage.data.continueAppend == 1) {
      this.setData({ //将携带的参数赋值
        reagentIds: currPage.data.reagentIds
      });
    }
    // 页面显示
    this.getIndexData();
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  getIndexData: function () {
    // 获取用户的登录信息
    let token = wx.getStorageSync('token');

    if (token) {
      let that = this;
      let reages = this.data.reagentIds;

      util.request(api.ChemreagentList).then(function (res) {
        if (res.code === 0) {
          let tmChemReagents = res.tmChemReagents.map(function (element, index, array) {
            // 是返回添加，默认选中
            if (reages && reages.indexOf(element.reagentId) > -1) {
              element.checked = true;
            }
            // 否则不选中
            if (!element.checked) {
              element.checked = false;
            }
            
            return element;
          });

          that.setData({
            tmChemReagents: tmChemReagents
          });
        }

        that.setData({
          checkedAllStatus: that.isCheckedAll()
        });
      });
    } else {
      wx.navigateTo({
        url: "/pages/cr/userCenter/login/login"
      });
    }
  },
  isCheckedAll: function () {
    //判断购物车商品已全选
    return this.data.tmChemReagents.every(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });
  },
  checkedItem: function (event) {
    let itemIndex = event.target.dataset.itemIndex;
    let that = this;

    let tmpTmChemReagents = this.data.tmChemReagents;
    // 赋值
    tmpTmChemReagents[itemIndex].checked = !tmpTmChemReagents[itemIndex].checked;

    this.setData({
      tmChemReagents: tmpTmChemReagents,
      checkedAllStatus: that.isCheckedAll(),
      'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
    });
  },
  getCheckedGoodsCount: function() {
    let checkedGoodsCount = 0;
    this.data.tmChemReagents.forEach(function (v) {
      if (v.checked === true) {
        checkedGoodsCount += 1;
      }
    });
    return checkedGoodsCount;
  },
  checkedAll: function () {
    let that = this;
    //编辑状态
    let checkedAllStatus = that.isCheckedAll();

    // 修改选择状态为全选或非全选
    let tmChemReagents = this.data.tmChemReagents.map(function (v) {
      v.checked = !checkedAllStatus;
      return v;
    });

    that.setData({
      tmChemReagents: tmChemReagents,
      checkedAllStatus: that.isCheckedAll(),
      'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
    });
  },
  checkoutOrder: function () {
    //获取已选择的商品
    let that = this;

    let tmChemReagents = this.data.tmChemReagents.filter(function (element, index, array) {
      if (element.checked == true) {
        return true;
      } else {
        return false;
      }
    });

    if (tmChemReagents.length <= 0) {
      util.showErrorToast('没有选择试剂！');
      return false;
    }
    let dataString = encodeURIComponent(JSON.stringify(tmChemReagents));

    wx.navigateTo({
      url: '../reagentApplyDetail/reagentApplyDetail?dataString=' + dataString
    })
  }
})